import pygame
import sys
from resources import Resources
from game_data import GameData
from ui_components import UIComponents
from game_state import GameState
from draw_manager import DrawManager
from game_logic import GameLogic
from constants import Constants
from core import Core


class Game:
    """主游戏类，协调所有组件"""

    def __init__(self):
        pygame.init()
        self.screen = pygame.display.set_mode((Constants.WIDTH, Constants.HEIGHT))
        pygame.display.set_caption("骗子酒馆--AI对局")

        self.resources = Resources()
        self.game_data = GameData()
        self.ui_components = UIComponents()
        self.game_state = GameState()
        self.draw_manager = DrawManager(self.resources, self.game_data, self.ui_components, self.game_state)
        self.game_logic = GameLogic(self.game_data, self.game_state)
        self.core = Core(self.game_data)
        # 将UI组件和资源传递给游戏逻辑
        self.game_logic.ui_components = self.ui_components
        self.game_logic.resources = self.resources

    def run(self):
        running = True
        game_printed = False
        while running:
            mouse_pos = pygame.mouse.get_pos()

            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    running = False

                elif event.type == pygame.MOUSEBUTTONDOWN:
                    if self.game_state.current_screen == "main":
                        # 主界面：检测按钮点击
                        if self.ui_components.button_start.collidepoint(event.pos):
                            self.game_state.current_screen = "init"
                        elif self.ui_components.button_history.collidepoint(event.pos):
                            print("查看对局记录（暂未实现）")
                        elif self.ui_components.button_exit.collidepoint(event.pos):
                            running = False

                    elif self.game_state.current_screen == "init":
                        self.game_logic.handle_init_screen_click(event.pos)


                elif self.game_state.current_screen == "game":
                    self.draw_manager.text_box_manager.handle_event(event)      # 处理文本框
                    if not game_printed:
                        # 首次进入游戏时的初始化
                        for i in range(4):
                            self.game_data.character_data_game[i] = {
                                "name": self.game_data.character_data_init[i]["name"],
                                "model": self.game_data.character_data_init[i]["model"],
                                "bullet_position_game": self.game_data.character_data_init[i]["bullet_position_init"],
                                "clip_capacity": self.game_data.character_data_init[i]["clip_capacity"],
                                "current_position": 0,  # 游戏开始位置归零
                                "hand": []  # 初始化空手牌
                            }

                        # 初始化牌组和发牌
                        self.core._initialize_deck()
                        self.core.deal_cards()
                        # 打印游戏状态
                        self.core.print_game_status()
                        game_printed = True

                elif event.type == pygame.KEYDOWN:
                    if self.game_state.input_active is not None:
                        if event.key == pygame.K_BACKSPACE:
                            self.game_data.input_values[self.game_state.input_active] = \
                                self.game_data.input_values[self.game_state.input_active][:-1]
                        else:
                            self.game_data.input_values[self.game_state.input_active] += event.unicode

                    if event.key == pygame.K_ESCAPE and self.game_state.current_screen == "init":
                        self.game_state.current_screen = "main"

            # 根据当前屏幕状态绘制不同的界面
            if self.game_state.current_screen == "main":
                self.draw_manager.draw_main_menu(self.screen, mouse_pos)
            elif self.game_state.current_screen == "init":
                self.draw_manager.draw_init_screen(self.screen, mouse_pos)
            elif self.game_state.current_screen == "game":
                self.draw_manager.draw_game_screen(self.screen)

            pygame.display.flip()

        pygame.quit()
        sys.exit()